<?php
/**
 * Created by PhpStorm.
 * User: luoshuqi
 * Date: 19-2-14
 * Time: 下午6:18
 */

namespace App\Utils;


use Gregwar\Captcha\CaptchaBuilder;

trait CaptchaTrait
{
    /**
     * 是否显示验证码
     * @return bool
     */
    private function displayCaptcha(): bool
    {
        return env('APP_ENV') === 'prod';
    }

    /**
     * 生成验证码
     * @return array
     */
    private function buildCaptcha(): array
    {
        $builder = new CaptchaBuilder();
        $builder->build();
        $captcha = strtoupper($builder->getPhrase());

        return [
            'content' => $builder->inline(),
            'signature' => hash_hmac('sha1', $captcha, env('APP_KEY'))
        ];
    }

    /**
     * 验证验证码
     * @param string $captcha
     * @param string $signature
     * @return bool
     */
    private function verifyCaptcha(string $captcha, string $signature): bool
    {
        return hash_hmac('sha1', strtoupper($captcha), env('APP_KEY')) === $signature;
    }
}